<?php

    /**
     * Authentification
     *
     * Main class to query the authentification system
     *
     * @package      Core
     * @subpackage   Authentification
     *
     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
     */
    class Core_Authentification extends Core_Object
    {

	////////////////////////////////////////////////////////////////////////////
	//                            Getters / Setters                           //
	////////////////////////////////////////////////////////////////////////////

		/**
		 * Returns the namespace
		 *
		 * @return		Zend_Session_Namespace
		 */
        public static function getNamespace()
        {
            $driver = self::_getDriver();
            return $driver->getNamespace();
        }

    ////////////////////////////////////////////////////////////////////////////
    //                             Static Methods                             //
    ////////////////////////////////////////////////////////////////////////////

		/**
		 * Connects a user
		 *
		 * @param       string		$username : the user's identifier
		 * @param       string		$password : the user's identifier
		 * @return      Zend_Auth_Result
		 */
        public static function login($username, $password)
        {
            $driver = self::_getDriver();
            return $driver->login($username, $password);
        }

		/**
		 * Disconnects a user
		 *
		 * @return      void
		 */
        public static function logoff()
        {
            $driver = self::_getDriver();
            return $driver->logoff();
        }

		/**
		 * Gets a value indicating wether the user is log in or not
		 *
		 * @return		boolean
		 */
        public static function isConnected()
        {
            $driver = self::_getDriver();
            return $driver->isConnected();
        }

    ////////////////////////////////////////////////////////////////////////////
    //                        Protected Static Methods                        //
    ////////////////////////////////////////////////////////////////////////////

        /**
         * Loads the authentification driver
         *
         * @throws      Core_Exception
         * @return      Core_Authentification_Interface
         */
        protected static function _getDriver()
        {
            static $driver = null;

            if ($driver === null) {
                $registry = Zend_Registry::getInstance();

                $driverName = ucfirst($registry['config']->drivers->authentification);
                $className = 'Drivers_'. $driverName .'_Authentification';
                $driver = new $className();

                if (!($driver instanceof Core_Authentification_Interface)) {
                    throw new Core_Exception('Fatal error: the given driver isn\'t an instance of Core_Authentification_Interface');
                }
            }

            return $driver;
        }
    }
